package BBDD;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import DAO.FamiliaDAO;
import DAO.GrupoDAO;

public class ManageFamilias {
	
		private Connection conn;
		private Statement pstmt;
		private ResultSet rs;
		private String query;
		private ManageDB abreConn;
		private Vector<FamiliaDAO> familia=new Vector<FamiliaDAO>();

		public ManageFamilias() {
			abreConn = new ManageDB();
			conn = abreConn.getConnection();
		
			abreConn.OpenDB();
		}

		public void loadDataFamilia(String query) {
			this.query = query;
			conn = abreConn.getConnection();
			
			try {
				pstmt = conn.createStatement();
				rs = pstmt.executeQuery(query);		
				
				while (rs.next()) {
					int idFamilia = rs.getInt(1);
					String descFamilia  = rs.getString(2);					
					setFamilia(new FamiliaDAO(idFamilia, descFamilia));

				}

			} catch (SQLException ex) {
				Logger.getLogger(ManageFamilias.class.getName()).log(Level.SEVERE,
						null, ex);
			} finally {
				try {
					rs.close();
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		}

		public void updateFamilias(String query) {
			this.query = query;

			conn = abreConn.getConnection();

			try {

				pstmt = conn.prepareStatement(query);
				pstmt.execute(query);
			} catch (SQLException ex) {
				Logger.getLogger(ManageFamilias.class.getName()).log(Level.SEVERE,
						null, ex);
			} finally {

				try {

					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		public void deleteFamilia(String query) {
			this.query = query;

			conn = abreConn.getConnection();

			try {

				pstmt = conn.prepareStatement(query);
				pstmt.execute(query);
			} catch (SQLException ex) {
				Logger.getLogger(ManageFamilias.class.getName()).log(Level.SEVERE,
						null, ex);
			} finally {

				try {

					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		public String getOldQuery() {
			return query;
		}

		public Vector<FamiliaDAO> getfamilia() {
			return familia;
		}

		public void setFamilia(FamiliaDAO familia) {
			this.familia.add(familia);
		}
}
